/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.report.template.Comparative;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ComparativeBroker;
import systole.view.messages.ErrorMessages;

/**
 *
 * @author jmj
 */
public class ComparativeBrokerDB extends BrokerDB implements ComparativeBroker {

    /**
     */
    public ComparativeBrokerDB() {
        super();
    }

    public void insert(Comparative comparative) throws ExceptionDAO {
        try {
            this.logger.logDebug("save comparative ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(comparative);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save comparative, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(Comparative comparative) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating comparative ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(comparative);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update comparative, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<Comparative> getAllComparatives() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all comparatives");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Comparative> list = currentSession.createQuery("from Comparative c order by upper(c.name)").list();
            this.logger.logDebug("getting comparatives successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all comparatives, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las Comparaciones", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existComparative(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist comparative");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Comparative.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List<Comparative> list = criteria.list();
            this.logger.logDebug("exist comparative successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist comparative, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la comparación", e.fillInStackTrace());
        }
    }
}
